export * from "./verify"
import { $http } from "@/common"

//获取当前日期函数
export const getNowFormatDate = () => {
	let date = new Date(),
		year = date.getFullYear(), //获取完整的年份(4位)
		month = date.getMonth() + 1, //获取当前月份(0-11,0代表1月)
		strDate = date.getDate() // 获取当前日(1-31)
	if (month < 10) month = `0${month}` // 如果月份是个位数，在前面补0
	if (strDate < 10) strDate = `0${strDate}` // 如果日是个位数，在前面补0
	return `${year}-${month}-${strDate}`
}

//获取当前完整日期
export const getCurrentDateTime = () => {
	const date = new Date()
	let y = date.getFullYear() //获取完整的年份(4位)
	let m = date.getMonth() + 1 //获取当前月份(0-11,0代表1月)
	let d = date.getDate() // 获取当前日(1-31)
	let h = date.getHours() //获取当前小时(0-23)
	let M = date.getMinutes() //获取当前分钟(0-59)
	let s = date.getSeconds() //获取当前秒(0-59)
	if (m < 10) m = `0${m}` // 如果月份是个位数，在前面补0
	if (d < 10) d = `0${d}` // 如果日是个位数，在前面补0
	if (h < 10) h = `0${h}` // 如果小时是个位数，在前面补0
	if (M < 10) M = `0${M}` // 如果分钟是个位数，在前面补0
	if (s < 10) s = `0${s}` // 如果秒是个位数，在前面补0
	return `${y}-${m}-${d} ${h}:${M}:${s}`
}

//格式化时间 '2023/9/01' --> 2023-09-01
export const formatDate1 = (value, str) => {
	const date = value
	let y = value.split(str)[0]
	let m = parseInt(value.split(str)[1])
	let d = parseInt(value.split(str)[2])
	if (m < 10) m = `0${m}` // 如果月份是个位数，在前面补0
	if (d < 10) d = `0${d}` // 如果月份是个位数，在前面补0
	return `${y}-${m}-${d}`
}

export const timestampToDateTime = (timestamp) => {
	const date = new Date(timestamp);
	const year = date.getFullYear();
	const month = ('0' + (date.getMonth() + 1)).slice(-2); // 月份是从0开始的
	const day = ('0' + date.getDate()).slice(-2);
	const hours = ('0' + date.getHours()).slice(-2);
	const minutes = ('0' + date.getMinutes()).slice(-2);
	return `${year}-${month}-${day} ${hours}:${minutes}`;
}

export const getUrlParam = (url, paramName) => {
	const params = url.split("?")[1].split("&");
	for (const param of params) {
		const [name, value] = param.split("=");
		if (name === paramName) {
			return value;
		}
	}
	return null;
}

export const generateUUIDv4 = () => {
	return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
		var r = (Math.random() * 16) | 0,
			v = c === 'x' ? r : (r & 0x3) | 0x8;
		return v.toString(16);
	});
}

export const timeSince = (timestamp) => {
	// 转换为毫秒
	timestamp *= 1000
	const now = new Date().getTime(); // 当前时间的时间戳
	const seconds = (now - timestamp) / 1000; // 计算差值，单位是秒

	let interval = seconds / 31536000; // 计算年数
	if (interval > 1) {
		return Math.floor(interval) + "年前";
	}
	interval = seconds / 2592000; // 计算月数
	if (interval > 1) {
		return Math.floor(interval) + "月前";
	}
	interval = seconds / 86400; // 计算天数
	if (interval > 1) {
		return Math.floor(interval) + "天前";
	}
	interval = seconds / 3600; // 计算小时
	if (interval > 1) {
		return Math.floor(interval) + "小时前";
	}
	interval = seconds / 60; // 计算分钟
	if (interval > 1) {
		return Math.floor(interval) + "分前";
	}
	return Math.floor(seconds) + "秒前"; // 秒数
}

export const toLogin = () => {
	return new Promise((resolve, reject) => {
			uni.login({
				provider: 'weixin', //使用微信登录
				onlyAuthorize: true,
				success: function(res) {
					console.log(res.code)
					$http.post('login', {
						appId: 'wx48a0b18199da5f27',
						code: res.code
					}).then(result => {
						console.log(result,'result')
						uni.setStorageSync('token', result.token)
						resolve()
						$http.get('userInfo').then(res2 => {
							  uni.setStorageSync('userInfo', JSON.stringify(res2))
						})
					})
				}
			})
	})
}
export const appId = 'wx48a0b18199da5f27'